import { HashMap } from "../../api/struct/HashMap";
import { E_DropActivityTag } from "../defs/drop/E_DropActivityTag";
import { CDDrop } from "../excel/CDDrop";
import { CDDropGroup } from "../excel/CDDropGroup";
import { SDActivity } from "../server/SDActivity";
import { Player } from "../struct/Player";
import { CEDropGroup } from "./CEDropGroup";


/**
 * 扩展-掉落
 */
export abstract class CEDrop {

    /**数据-掉落组 */
    private static readonly dropTeam = new HashMap<number, CDDrop[]>();


    /**初始化 */
    public static init(): void {
        const vs = CDDrop.datas.getValues();
        for (let i = 0, len = vs.length; i < len; i++) {
            let list = this.dropTeam.get(vs[i].dropid);
            if (!list) {
                list = [];
                this.dropTeam.put(vs[i].dropid, list);
            }
            list.push(vs[i]);
        }
    }


    /** 获取数据*/
    public static getTeamData(teamId: number): CDDrop[] {
        return this.dropTeam ? this.dropTeam.get(teamId) : null;
    }


    /**
     * 获取可能掉落
     * @param dropGroupId 
     */
    public static getPossibleDropConfigList(
        player: Player,
        dropGroupId: number,
        outList: CDDrop[] = []
    ): CDDrop[] {

        const groupConfig = CDDropGroup.getData(dropGroupId);

        //有多组的时候-每件都掉
        if (groupConfig) {
            const teamList = CEDropGroup.getTeamList(groupConfig.dropGroupid);
            if (teamList && teamList.length > 0) {
                for (let n = 0, len = teamList.length; n < len; n++) {
                    const list = CEDrop.getTeamData(teamList[n]);
                    if (!list) {
                        continue;
                    }
                    for (let i = 0, len = list.length; i < len; i++) {
                        const dropConfig = list[i];
                        if (outList.indexOf(dropConfig) >= 0) {
                            continue;
                        }

                        if (dropConfig.activityTag === E_DropActivityTag.AddDrop) {
                            if (groupConfig.activityid === 0
                                || !SDActivity.isOpen(player, groupConfig.activityid)) {
                                continue;       //没有开启的活动不掉落
                            }
                        }

                        outList.push(list[i]);
                    }
                }
            }
        }

        //没有分组，就掉落自身
        if (0 >= outList.length) {
            let teamList = CEDrop.getTeamData(dropGroupId);
            if (!teamList) {
                const dropConfig = CDDrop.getData(dropGroupId);
                if (dropConfig) {
                    teamList = CEDrop.getTeamData(dropConfig.dropid);
                }
            }
            if (teamList) {
                for (let i = 0, len = teamList.length; i < len; i++) {
                    const dropConfig = teamList[i];
                    if (outList.indexOf(dropConfig) >= 0) {
                        continue;
                    }
                    if (dropConfig.activityTag === E_DropActivityTag.AddDrop) {
                        if (groupConfig.activityid === 0
                            || !SDActivity.isOpen(player, groupConfig.activityid)) {
                            continue;       //没有开启的活动不掉落
                        }
                    }

                    outList.push(dropConfig);
                }
            }
        }

        if (0 >= outList.length) {
            console.error('掉落配置 group id = ' + dropGroupId + '， 0 掉落');
        }

        return outList;
    }



}
